﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using ClipperBaseUI.Controls;
using System.Collections.ObjectModel;
using Clipper.BLL;
using System.Data;
using Clipper.Model.ViewModel;
using SampleModel = Clipper.Model.SampleInfo;
using System.ComponentModel;
using System.Threading;

namespace Clipper
{
    /// <summary>
    /// WinPatientInfo.xaml 的交互逻辑
    /// </summary>
    public partial class WinPatientInfo : BaseWindow
    {
        #region 变量
        PatientSampleVM patientVM = null;
        SampleInfo mSampleInfoBll = null;
        Model.SampleInfo mSample = null;
        /// <summary>
        /// 输入状态
        /// </summary>
        Model.InputEnum InputState = 0;

        #endregion
        public WinPatientInfo()
        {
            InitializeComponent();

        }
        private void BaseWindow_Loaded(object sender, RoutedEventArgs e)
        {
            ctrlBusy.IsBusy = true;
            BackgroundWorker backWorker = new BackgroundWorker();
            backWorker.DoWork += (ss, ee) =>
                {
                    patientVM = new PatientSampleVM();
                    mSampleInfoBll = new SampleInfo();
                    patientVM.SampleList = mSampleInfoBll.GetFullSampleInfoList("SampleID<>0");
                    detectItemSet1.GetItemData();
                };
            backWorker.RunWorkerCompleted += (ss, ee) =>
                {
                    datPatient.ItemsSource = patientVM.SampleList;
                    detectItemSet1.ItemDataBind();
                    this.DataContext = patientVM;
                    ctrlBusy.IsBusy = false;
                };
            backWorker.RunWorkerAsync();
            dtpFrom.SelectedDate = DateTime.Now;
            dtpTo.SelectedDate = DateTime.Now;
            dtpTo.DisplayDateStart = DateTime.Now;
            btnSave.IsEnabled = false;
            btnCancel.IsEnabled = false;
        }

       
       
        #region 清空，搜索，添加，修改，删除,保存按钮事件
        //清空
        private void btnClear_Click(object sender, RoutedEventArgs e)
        {
            patientVM.SelectedItem.Clear();
        }

        //搜索
        private void btnSearch_Click(object sender, RoutedEventArgs e)
        {
            string from = dtpFrom.SelectedDate.Value.ToString("yyyy-MM-dd");
            string to = dtpTo.SelectedDate.Value.ToString("yyyy-MM-dd");
            datPatient.ItemsSource=mSampleInfoBll.GetSampleList(from, to);
        }
        //添加
        private void btnAdd_Click(object sender, RoutedEventArgs e)
        {
            if (patientVM.NewSample.Count == 0)
            {
                InputState = Model.InputEnum.Add;
                btnSave.IsEnabled = true;
                btnCancel.IsEnabled = true;
                long _sampleId = mSampleInfoBll.GetLastSampleId()+1;
                patientVM.NewSample.Add(new SampleModel() { sampleID = _sampleId });
               
            }
        }
        //修改
        private void Modify_Click(object sender, RoutedEventArgs e)
        {
            if (mSample != null)
            {
                InputState = Model.InputEnum.Update;
                btnSave.IsEnabled = true;
                btnCancel.IsEnabled = true;
                patientVM.NewSample = new ObservableCollection<SampleModel>() { mSample };
                if (mSample.DetectItems != null)
                {
                    patientVM.SelectedItem.Clear();
                    foreach (Model.DetectItems item in mSample.DetectItems)
                    {
                        patientVM.SelectedItem.Add(item);
                    }
                }

            }
            else
            {
                UIHelper.ShowKeyMessage("SelectSample");
            }

        }
        //删除
        private void Delete_Click(object sender, RoutedEventArgs e)
        {
            if (mSample != null)
            {
                if (UIHelper.ShowKeyAskMessage("IsDelete"))
                {
                    mSampleInfoBll.Delete(mSample.sampleID);
                    datPatient.ItemsSource = mSampleInfoBll.GetFullSampleInfoList("sampleID<>0");
                    mSample = null;
                }
            }
            else
            {
                UIHelper.ShowKeyMessage("SelectSample");
            }
        }
        /// <summary>
        /// 保存
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Save_Click(object sender, RoutedEventArgs e)
        {
            ObservableCollection<SampleModel> newSample = datPatientInput.Items.SourceCollection as ObservableCollection<SampleModel>;
            ObservableCollection<Model.DetectItems> detectItems = lsvDetectItemNew.Items.SourceCollection as ObservableCollection<Model.DetectItems>;
            
                if (newSample != null && detectItems != null)
                {
                    if (InputState==Model.InputEnum.Add)
                    {
                        newSample[0].DetectItems = detectItems;
                        newSample[0].createTime = DateTime.Now;
                        if (UIHelper.SuccessOrFailTip(mSampleInfoBll.Add(newSample[0]), "AddTip"))
                        {
                            datPatient.ItemsSource = mSampleInfoBll.GetFullSampleInfoList("SampleID<>0");
                            CompleteSave();
                        }
                    }
                    else
                    {
                        if (!mSampleInfoBll.Exists(newSample[0].sampleID))
                        {
                            newSample[0].DetectItems = detectItems;
                            if (UIHelper.SuccessOrFailTip(mSampleInfoBll.Update(newSample[0]), "ModifyTip"))
                            {
                                datPatient.ItemsSource = mSampleInfoBll.GetFullSampleInfoList("SampleID<>0");
                                CompleteSave();
                            }
                        }
                        else
                        {
                            UIHelper.ShowKeyMessage("RepeatSample");
                        }
                    }
                }
           
        }

        //取消
        private void btnCancel_Click(object sender, RoutedEventArgs e)
        {
            CompleteSave();
        }

        private void CompleteSave()
        {
            patientVM.SelectedItem.Clear();
            InputState = 0;
            btnSave.IsEnabled = false;
            btnCancel.IsEnabled = false;
            patientVM.NewSample.Clear();
        }
        #endregion

        #region 其它事件
        //编辑样本的选择项列表双击事件
        private void lsvDetectItemSelected_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            Model.DetectItems itemCancel = lsvDetectItemNew.SelectedItem as Model.DetectItems;
            if (itemCancel != null)
            {
                (lsvDetectItemNew.ItemsSource as ObservableCollection<Model.DetectItems>).Remove(itemCancel);
            }

        }

        private void datPatient_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (e.AddedItems.Count > 0)
            {
                mSample = e.AddedItems[0] as Model.SampleInfo;
                if (mSample != null)
                {
                    //lsvDetectItemSaved.ItemsSource = mSampleInfoBll.GetSampleDetectItems(sample.sampleID);
                }
            }

        }
        private void dtpFrom_LostFocus(object sender, RoutedEventArgs e)
        {
            dtpTo.DisplayDateStart = dtpFrom.SelectedDate;
        }

        private void dtpTo_LostFocus(object sender, RoutedEventArgs e)
        {
            dtpFrom.DisplayDateEnd = dtpTo.SelectedDate;
        }
        private void detectItemSet1_SelectionChangedEvent(object sender, SelectionChangedEventArgs e)
        {
            if (detectItemSet1.CurrentItem != null&&!patientVM.SelectedItem.Contains(detectItemSet1.CurrentItem))
            {
                patientVM.SelectedItem.Add(detectItemSet1.CurrentItem);
            }
        }
        #endregion

       
    }
}
